package com.zzl.alligator.management.system.service.impl;


import com.zzl.alligator.management.system.domain.SysDictType;
import com.zzl.alligator.management.system.mapper.SysDictTypeMapper;
import com.zzl.alligator.management.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 字典类型表(SysDictType)服务层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@Service
@CacheConfig(cacheNames = "DICT_CACHE")
@Slf4j
public class SysDictTypeService implements ISysDictTypeService {
    
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    
    /**
     * 通过ID查询单条数据
     *
     * @param dictId 主键
     * @return 实例对象
     */
    @Override
    public SysDictType findSysDictTypeById(Long dictId) {
        return sysDictTypeMapper.findSysDictTypeById(dictId);
    }

   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysDictType 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysDictType> findSysDictTypeList(SysDictType sysDictType) {
        return sysDictTypeMapper.findSysDictTypeList(sysDictType);
    }

    /**
     * 新增数据
     *
     * @param sysDictType 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysDictType(SysDictType sysDictType) {
        return sysDictTypeMapper.insertSysDictType(sysDictType);
    }

    /**
     * 修改数据
     *
     * @param sysDictType 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#sysDictType.dictType")
    public int updateSysDictType(SysDictType sysDictType) {
        return sysDictTypeMapper.updateSysDictType(sysDictType);
    }

    /**
     * 通过主键删除数据
     *
     * @param dictId 主键
     * @param dictType 字典类型
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#dictType")
    public int deleteById(String dictType, Long dictId) {
        return sysDictTypeMapper.deleteById(dictId);
    }


    /**
     * 清空缓存
     */
    @Override
    @CacheEvict(allEntries = true)
    public void clearCache() {
        log.info("清除所有字典缓存");
    }
}